Core.Test.registerTest({
	group: "storage",
	name: "Singleton.StorageRules.getNormalizedPath()",
	
	before: function() {
		Core.App.stop();
		this.rules = Singleton.newInstance("core.storage.rule");
	},
	
	after: function() {
		Core.App.start();
	},
	
	"Should find a point for direct rule (path same as rule)": function() {
		this.rules.register("cases.path");
		Assert.equal("cases.path", this.rules.getNormalizedPath(["cases", "path"]));
	},
	
	"Should find a point for direct rule (path longer than rule)": function() {
		this.rules.register("cases.path");
		Assert.equal("cases.path", this.rules.getNormalizedPath(["cases", "path", "to", "hell"]));
	},
	
	"Should find an empty point for direct rule (path different than the rule)": function() {	
		this.rules.register("cases.path");
		Assert.equal("", this.rules.getNormalizedPath(["cases", "another", "path"]));
	},
	
	"Should find the closest point for given path for many direct rules (rules completly different)": function() {
		this.rules.register("test.path");
		this.rules.register("cases.path");
		this.rules.register("cover.path");

		Assert.equal("cases.path", this.rules.getNormalizedPath(["cases", "path"]));
		Assert.equal("test.path", this.rules.getNormalizedPath(["test", "path", "2"]));
	},
	
	"Should find the closest point for given path for many direct rules (rules overlaps)": function() {
		this.rules.register("cases");
		this.rules.register("cases.path");
		this.rules.register("cases.path.to.hell");
		
		Assert.equal("cases", this.rules.getNormalizedPath(["cases"]));
		Assert.equal("cases.path", this.rules.getNormalizedPath(["cases", "path"]));
		Assert.equal("cases.path.to.hell", this.rules.getNormalizedPath(["cases", "path","to", "hell", "again"]));
	},
	
	"Should find an empty point if path doesn't match to any of the rules": function() {
		this.rules.register("test.path");
		this.rules.register("cases.path");
		
		Assert.equal("", this.rules.getNormalizedPath(["another", "path"]));
	},
	
	"Should find a point for child rule (path is 1lvl longer than rule)": function() {
		this.rules.register("cases.path.*");
		
		Assert.equal("cases.path.1", this.rules.getNormalizedPath(["cases", "path", "1"]));
		Assert.equal("cases.path.2", this.rules.getNormalizedPath(["cases", "path", "2"]));
		Assert.equal("cases.path.5", this.rules.getNormalizedPath(["cases", "path", "5"]));
	},
	
	"Should find a point for child rule (path is 2lvls longer than rule)": function() {
		this.rules.register("cases.path.*");
		
		Assert.equal("cases.path.1", this.rules.getNormalizedPath(["cases", "path", "1", "dada"]));
		Assert.equal("cases.path.2", this.rules.getNormalizedPath(["cases", "path", "2", "fafa"]));
	},
	
	"Should find an empty point for child rule (path same as rule)": function() {
		this.rules.register("cases.path.*");
		Assert.equal("", this.rules.getNormalizedPath(["cases", "path"]));
	},
	
	"Should find an empty point for child rule (path different than the rule)": function() {
		this.rules.register("cases.path.*");
		Assert.equal("", this.rules.getNormalizedPath(["another", "path"]));
	},

	"Should find a point for many mixed rules": function() {
		this.rules.register("cases.path");
		this.rules.register("cases.path.*");
		
		Assert.equal("cases.path", this.rules.getNormalizedPath(["cases", "path"]));
		Assert.equal("cases.path.5", this.rules.getNormalizedPath(["cases", "path", "5"]));
		Assert.equal("cases.path.13", this.rules.getNormalizedPath(["cases", "path", "13", "title"]));
	},
	
	"Should find a point for recursive child rule (path is 2lvl longer than rule)": function() {
		this.rules.register("path.*.*");
		Assert.equal("path.child.child", this.rules.getNormalizedPath(["path", "child", "child"]));
	},

	"Should find a point for any rule followed by direct property": function() {
		this.rules.register("path.*.id");
		Assert.equal("path.0.id", this.rules.getNormalizedPath(["path", "0", "id"]));
		Assert.equal("path.1.id", this.rules.getNormalizedPath(["path", "1", "id"]));
		
		Assert.equal("path.1.id", this.rules.getNormalizedPath(["path", "1", "id", "dada"]));
	},
	
	"Shouldn't find a point if direct property after any rule does not match (1)": function() {
		this.rules.register("path.*.id");
		Assert.equal("", this.rules.getNormalizedPath(["path", "1", "other", "dada"]));
	},
	
	"Shouldn't find a point if direct property after any rule does not match (1)": function() {
		this.rules.register("path.*.id");
		Assert.equal("", this.rules.getNormalizedPath(["path", "1", "id2", "dada"]));
	}
});